package structure.proxy;

import java.util.concurrent.TimeUnit;

/**
 * @author lzp
 * @Date:2023/2/27
 * @description: 打印机类
 */
public class Printer implements Printable {
	private String name;

	public Printer(String name) {
		this.name = name;
		this.heavyJob("正在生成Printer的实例=>" + name);
	}

	@Override
	public synchronized void setPrinterName(String name) {
		this.name = name;
	}

	@Override
	public String getPrinterName() {
		return name;
	}

	@Override
	public void print(String string) {
		// 先输出打印机的名字，再执行打印方法
		System.out.printf("==================== %s ====================\n", name);
		System.out.println(string);
	}

	/**
	 * 模拟一个耗时实例初始化动作
	 *
	 * @param msg
	 */
	private void heavyJob(String msg) {
		System.out.println(msg);
		for (int i = 0; i < 5; i++) {
			try {
				Thread.sleep(TimeUnit.SECONDS.toMillis(1));
			} catch (InterruptedException e) {
			}
			System.out.print(".");
		}
		System.out.println("结束。");
	}
}
